package com.wudi.estate.service.impl.servicemode;

import com.wudi.estate.bean.communitymanagement.Community;
import com.wudi.estate.bean.homemanage.Buildings;
import com.wudi.estate.bean.ownermanagement.Personnel;
import com.wudi.estate.bean.servicemode.MailBox;
import com.wudi.estate.mapper.communitymanagement.CommunityMapper;
import com.wudi.estate.mapper.homemanage.BuildingsMapper;
import com.wudi.estate.mapper.ownermanagement.PersonnelMapper;
import com.wudi.estate.mapper.servicemode.MailBoxMapper;
import com.wudi.estate.service.servicemode.MailBoxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MailBoxServiceImpl implements MailBoxService {
    @Autowired
    private MailBoxMapper mailBoxMapper;
    @Autowired
    private PersonnelMapper personnelMapper;
    @Autowired
    private BuildingsMapper buildingsMapper;
    @Autowired
    private CommunityMapper communityMapper;
//    查询所有邮件 （显示列表）
    @Override
    public List<Map<String, Object>> selectAll() throws Exception {
        List<Map<String,Object>> lists = new ArrayList<>();
        List<MailBox> mailBoxesox  = mailBoxMapper.selectList(null);
        for (MailBox mailBox : mailBoxesox) {
            Map<String,Object> maps = new HashMap<>();
            maps.put("mailBoxById",mailBox);
//            根据信箱的pid获取personnel的信息
            Personnel personnelByPid = personnelMapper.selectById(mailBox.getPid());
            maps.put("personnelByPid",personnelByPid);
//            根据personnel的bid获取Buildings的信息
            Buildings buildingsByBid = buildingsMapper.selectById(personnelByPid.getBid());
//            根据Buildings的cid获取小区的信息；
            Community communityByCid = communityMapper.selectById(buildingsByBid.getCid());
            maps.put("communityByCid",communityByCid);
            lists.add(maps);
        }
        return lists;
    }
    //    查询信箱的小区和人员  信箱添加显示
    @Override
    public Map<String, Object> selectMailBoxAndCommunityAndPersonnel() throws Exception{
        Map<String,Object> maps = new HashMap<>();
        List<MailBox> mailBoxesailBox = mailBoxMapper.selectList(null);
        List<Community> communities = communityMapper.selectList(null);
        List<Personnel> personnels = personnelMapper.selectList(null);
        maps.put("communities",communities);
        maps.put("personnels",personnels);
        return maps;
    }
//    信箱添加
    @Override
    public void insert(MailBox mailBox) throws Exception {
            mailBoxMapper.insert(mailBox);
    }
    //  信箱编辑显示
    @Override
    public Map<String, Object> selectMailBoxAndPersonnel(long id) throws Exception{
        Map<String,Object> maps = new HashMap<>();
        MailBox mailBox = mailBoxMapper.selectById(id);
        maps.put("mailBox",mailBox);
        List<Personnel> personnels = personnelMapper.selectList(null);
        maps.put("personnels",personnels);
        List<Community> communities = communityMapper.selectList(null);
        maps.put("communities",communities);
        return maps;
    }
//    信箱修改
    @Override
    public void update(MailBox mailBox) {
        mailBoxMapper.updateById(mailBox);
    }
    //  把指定的id的状态更改（修改用）
    @Override
    public void deleteById(long id) throws Exception {
        MailBox mailBox = mailBoxMapper.selectById(id);
        mailBox.setState(false);
        mailBoxMapper.updateById(mailBox);
    }
}
